/*
 *  SentenceSplitter.java
 *
 *
 *  Copyright (c) 2007
 *  Knowledge Information & Data Management Laboratory
 *  Sirindhorn International Institute of Technology
 *  Thammasat University
 * 
 *  Arthit Suriyawongkul
 *  art <at> siit.net
 *
 *  art, 16/7/2007
 *
 *  $Id: SentenceSplitter.jav 2820 2001-11-14 17:15:43Z oana $
 */

package net.siit.gate;

import java.util.*;
import java.net.URL;

import gate.*;
import gate.creole.*;
import gate.util.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import net.siit.util.DataFactory;
import org.speedblue.util.Trie;

/**
 * This class is the implementation of the resource DICTIONARYBASEDPOSTAGGER.
 */
public class SentenceSplitter  extends AbstractLanguageAnalyser
        implements ProcessingResource {
    
    private Trie dict;
    private int maxLength;
    private int minLength;
    
    /*
     * this method gets called whenever an object of this
     * class is created either from GATE GUI or if
     * initiated using Factory.createResource() method.
     */
    public Resource init() throws ResourceInstantiationException {                
        fireStatusChanged("Creating a sentence splitter...");
        fireProgressChanged(0);
        
        if ((dictionaryURL == null) || (dictionaryURL.toString().trim().length() == 0)) {
            throw new ResourceInstantiationException("Dictionary URL must be specified.");
        }
        if ((dictionaryEncoding == null) || (dictionaryEncoding.trim().length() == 0)) {
            fireStatusChanged("Unsupported encoding, use UTF-8.");
            dictionaryEncoding = "UTF-8";
        }
        fireProgressChanged(5);
        
        try {
            dict = DataFactory.createTrieDictionary(dictionaryURL, dictionaryEncoding);
            maxLength = dict.getMaxLength();
            minLength = dict.getMinLength();
        } catch (URISyntaxException ex) {
            throw new gate.creole.ResourceInstantiationException("Dictionary URL is file not found: ", ex);
        } catch (UnsupportedEncodingException ex) {
            throw new ResourceInstantiationException("Dictionary encoding is not supported: " + dictionaryEncoding, ex);
        } catch (FileNotFoundException ex) {
            throw new gate.creole.ResourceInstantiationException("Not found dictionary at a given URL: ", ex);
        } catch (IOException ex) {
            throw new gate.creole.ResourceInstantiationException(ex);
        }
        fireProgressChanged(100);

        fireStatusChanged("Instantiated");
        fireProcessFinished();
        return this;
    }

    /*
     * this method should provide the actual functionality of the PR
     * (from where the main execution begins). This method
     * gets called when user click on the RUN button in the
     * GATE GUIs application window.
     */
    public void execute() throws ExecutionException {
        if (document == null) {
            throw new ExecutionException("No document to process!");
        }

        fireStatusChanged("Sentence splitting " + document.getName() + "...");

        if (annotationSetName != null && annotationSetName.trim().length() == 0) {
            annotationSetName = null;
        }

        AnnotationSet as = (annotationSetName == null) ?
            document.getAnnotations(null) :
            document.getAnnotations(annotationSetName);
        if (as == null || as.size() == 0) {
            throw new GateRuntimeException("AnnotationSet \"" +
                    annotationSetName + "\" is null or empty.");
        }
        
        AnnotationSet tokenAS =
                as.get(ANNIEConstants.TOKEN_ANNOTATION_TYPE);
        if (tokenAS == null || tokenAS.size() == 0) {
            throw new GateRuntimeException(
                    "No Tokens found. Please run tokeniser first.");
        }

        AnnotationSet sentenceAS =
                as.get(ANNIEConstants.SENTENCE_ANNOTATION_TYPE);

        //!! lsHERE

        for (Annotation token : tokenAS) {
            FeatureMap feat = token.getFeatures();

            String string = (String) feat.get(TOKEN_STRING_FEATURE_NAME);
        }
        
        // dereference
        as = null;

        fireStatusChanged("Sentence splitting complete!");
        fireProgressChanged(100);
        fireProcessFinished();
    }

    /* this method is called to reinitialize the resource */
    public void reInit() throws ResourceInstantiationException {
        this.init();
    }
    
    
   /*
    * There are two types of parameters
    * 1. Init time parameters values for these parameters need to be
    * provided at the time of initializing a new resource and these values are
    * not supposed to be changed.
    * 2. Runtime parameters - values for these parameters are provided at the time
    * of executing the PR. These are runtime parameters and can be
    * changed before starting the execution
    * (i.e. before you click on the "RUN" button in the GATE GUI)
    * It is must to provide setter and getter methods for every such
    * parameter declared in the creole.xml.
    *
    * for example to set a value for outputAnnotationSetName
    */
    private String annotationSetName;
    private URL dictionaryURL;
    private String dictionaryEncoding;

    // getters, setters
    public String getAnnotationSetName() {
        return annotationSetName;
    }
    public void setAnnotationSetName(String annotationSetName) {
        this.annotationSetName = annotationSetName;
    }

    public URL getDictionaryURL() {
        return dictionaryURL;
    }
    public void setDictionaryURL(URL dictionaryURL) {
        this.dictionaryURL = dictionaryURL;
    }
    
    public String getDictionaryEncoding() {
        return dictionaryEncoding;
    }
    public void setDictionaryEncoding(String dictionaryEncoding) {
        this.dictionaryEncoding = dictionaryEncoding;
    }

}
